<?php

 Zend_Loader::loadClass("BaseControllerAuth");
 Zend_Loader::loadClass("AuthUtility");
 class AuthController extends BaseControllerAuth
 {
 	public function initialize()
 	{
		$this->setLayout("index");
		$this->assign("pageTitle","Trade " . $this->getConfig()->client->name);
		
		$this->byPassAuth("login");
		$this->byPassAuth("logout");
		$this->byPassAuth("forgotp");
		$this->byPassAuth("userinforesend");
		$this->byPassAuth("resetpass");
		$this->byPassAuth("reset");
		$auth = BaseAuth::getInstance();
		$auth->setTableName("users");
		$auth->setIdentityColumn("username");
		$auth->setCredentialColumn("password");
		$auth->storeData(array("id","username","user_type_id","cc_key","deleted","org_id"));
 	}
	
	
 	public function loginAction()
 	{
		
		$username = $this->getPost("username");
		$password = $this->getPost("password");
		$login = $this->getPost("login");
		$ret = $this->getParam("ret");
		$message = "";
		if($login){

			$auth = BaseAuth::getInstance();
			$auth->authenticate($username, AuthUtility::hashPassword($password));
			if($auth->authenticated() && !$auth->deleted)
			{

				if($this->getConfig()->auth->expiry){	
					$auth->setExpiryMinutes($this->getConfig()->auth->expiry);
				}else{
					$auth->setExpiryMinutes(120);
				}
				Zend_Loader::loadClass("CodesUtility");
				Zend_Loader::loadClass("UserTypes");
				$userTypes = new UserTypes();
				$userTypesArray = $userTypes->getUserTypesArray("code", "id");
				switch($auth->user_type_id) { 
					case $userTypesArray[CodesUtility::USER_TYPE_IMPORTER]:
						$this->_redirect("/importer/index/");
						break;	
					case $userTypesArray[CodesUtility::USER_TYPE_EXPORTER]:
						$this->_redirect("/exporter/index/");
						break;	
					case $userTypesArray[CodesUtility::USER_TYPE_ADMIN]:
						$this->_redirect("/admin/index/");
						break;
					case $userTypesArray[CodesUtility::USER_TYPE_NFA]:
						$this->_redirect("/nfa/index/");
						break;
					case $userTypesArray[CodesUtility::USER_TYPE_GOVERNMENT]:
						$this->_redirect("/government/index/");
						break;						
				}
				
			}
			
			if($auth->deleted){
				$auth->deAuthenticate();
			}
			$message = "Login Error";
		}
		
		$this->assign("username",$username);
		$this->assign("message",$message);
		if($ret){
			Zend_Loader::loadClass("GeneralSettings");
			Zend_Loader::loadClass("SetupUtility");
			
			$generalSettings = new GeneralSettings();
			$gensettings = $generalSettings->getGeneralSettingsArray("code","value",array(SetupUtility::CONTACT_PHONE));
			$this->assign("contactphone",$gensettings[SetupUtility::CONTACT_PHONE]);
			$this->render('loginret.tpl');
		}else{	
			$this->render();
		}
 	}
	
	public function logoutAction()
	{
		Zend_Loader::loadClass("CartUtility");
		
		$cart = CartUtility::getAdminCart();
		$cart->items = null;
		
		
		$cart = CartUtility::getOrderCart();
		$cart->items = null;
		
		$cart = CartUtility::getPublicCart();
		$cart->items = null;
		
		$this->deAuthenticate();
		$this->assign("message","You have been logged out.");
		$this->render('login.tpl');
	}
	
 	public function deAuthenticate()
 	{
		$auth = BaseAuth::getInstance();
		$auth->deAuthenticate();
 	}
	
	
 }
?>